﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Windows.Forms;
using System.IO;

namespace TooltracePC
{
    class dbf
    {
        private static string _strConn = "";
        OdbcConnection _conn = new OdbcConnection();
        private static string _dbPath = "";



        public dbf(string path)
        {
            _dbPath = path;
        }


        public int openDB()
        {
            int result = 0;
            if (string.IsNullOrEmpty(_dbPath))
            {
                return result;
            }

            _strConn = @"PWD=;SourceType=DBF;UID=;Collate=Machine;Deleted=Yes;BackgroundFetch=Yes;Exclusive=No;Driver=Microsoft Visual FoxPro Driver;Null=Yes;SourceDB=" + _dbPath;
            try
            {
                _conn.ConnectionString = _strConn;
                _conn.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return result;
            }
            //closeDB();
            return 1;
        }



        //close
        private void closeDB()
        {
            _conn.Close();
            _conn.Dispose();
        }



        //exeNonQuery
        public int executeNonQuery(string sql)
        {
            if(openDB() == 0)
            {
                return 0;
            }

            OdbcCommand cmd = new OdbcCommand();
            cmd.Connection = _conn;

            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }

            closeDB();
            return 1;
        }


        //get all
        public DataTable getAll(string tableName)
        {
            //DataTable dt = null;
            if (openDB() == 0)
            {
                return null;
            }
            string sql = "select * from " + tableName;
            OdbcDataAdapter da = new OdbcDataAdapter(sql, _conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            closeDB();
            return ds.Tables[0];
        }


  


    }
}
